package com.meme.editor.renderer;


import com.meme.editor.gui.sprite2d.Tiled2dLayerStatus;
import com.meme.editor.model.sprite2d.Tiled2dBackground;
import com.meme.editor.model.sprite2d.Tiled2dLayer;
import com.meme.editor.model.sprite2d.Tiled2dLayerCell;
import com.meme.editor.gui.sprite2d.Tiled2dLayerListCellRenderer;

import java.awt.Graphics2D;

public class Tiled2dBackgroundRenderer 
{
	public static void renderTiled2dBackground (Graphics2D g, Tiled2dBackground background, int posX, int posY, int scale)
	{
		if (background == null)
			return;

		int cellRows   = background.getCellRows();
		int cellCols   = background.getCellCols();
		int cellWidth  = background.getCellWidth();
		int cellHeight = background.getCellHeight();

		int scaledW = cellWidth  * scale;
		int scaledH = cellHeight * scale;

		for (int layerID = 0; layerID < background.getLayersCount(); ++ layerID)
		{
			Tiled2dLayer layer = background.getLayerByIndex (layerID);
			Tiled2dLayerStatus layerStatus = Tiled2dLayerListCellRenderer.getTiled2dLayerStatus (layer);
			if ( ! layerStatus.isVisible() )
				continue;

			for (int row = 0; row < cellRows; ++ row)
			{
				for (int col = 0; col < cellCols; ++ col)
				{
					int x = posX + scaledW * col;
					int y = posY + scaledH * row;
				
					Tiled2dLayerCell layerCell = layer.getLayerCellByPos(row, col);
					if (layerCell.getModule() != null)
						layerCell.getModule().render(g, x, y, layerCell.isFlipX(), layerCell.isFlipY(), scale, 0);
				}
			}
		}
	}
}
